package online.westonline.com.westonline.ui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import online.westonline.com.westonline.R;
import online.westonline.com.westonline.adapter.WorkAdapter;
import online.westonline.com.westonline.base.BaseFragment;
import online.westonline.com.westonline.entry.WorkItem;
import online.westonline.com.westonline.ui.ConstactsActivity;
import online.westonline.com.westonline.ui.MeetingActivity;
import online.westonline.com.westonline.ui.NodeTypeActivity;
import online.westonline.com.westonline.ui.WorkPlanListActivity;
import online.westonline.com.westonline.utils.CacheUtils;
import online.westonline.com.westonline.utils.OnRecyclerItemClickListener;

/**
 * 工作
 * Created by xiatao on 2018/7/31.
 */

public class WorkFragment extends BaseFragment {
    @BindView(R.id.recycler_work)
    RecyclerView recyclerWork;
    Unbinder unbinder;
    private List<WorkItem> workItems = new ArrayList<>();
    private ItemTouchHelper touchHelper;
    private WorkAdapter workAdpter;
    @Override
    protected boolean setEventOpen() {
        return false;
    }

    @Override
    protected void initdata() {
        //直接生成不会出现图片乱码
        workItems = CacheUtils.getInstance().initworkmode();
        CacheUtils.getInstance().setWorkItemPosition(workItems);
    }


    @Override
    protected void initview() {
        initrecyclerview();
        cofigtouch();
    }
    //初始化列表视图
    private void initrecyclerview() {
        workAdpter = new WorkAdapter();
        recyclerWork.setLayoutManager(new GridLayoutManager(getActivity(),2,GridLayoutManager.VERTICAL,false));
        recyclerWork.setAdapter(workAdpter);
        workAdpter.setDatas(workItems);
        workAdpter.notifyDataSetChanged();
        //设置最后一位(更多不能拖动)
        recyclerWork.addOnItemTouchListener(new OnRecyclerItemClickListener(recyclerWork) {
            @Override
            public void onItemClick(RecyclerView.ViewHolder vh) {

            }
            @Override
            public void onLongClick(RecyclerView.ViewHolder vh) {
                //不是最后一位则执行拖拽
                //如果item不是最后一个，则执行拖拽
                    touchHelper.startDrag(vh);
            }
        });
        workAdpter.setClickListener(new WorkAdapter.onClickListener() {
            @Override
            public void onClick(int pos, WorkItem workItem) {
                if (workItem.getWid()==6)
                {
                    startActivity(NodeTypeActivity.class,false);
                }
                if (workItem.getWid()==5)
                {
                    startActivity(WorkPlanListActivity.class,false);
                }
                if (workItem.getWid()==1)
                {
                    startActivity(MeetingActivity.class,false);
                }
                if (workItem.getWid()==0)
                {
                    startActivity(ConstactsActivity.class,false);
                }
            }
        });

    }
    //配置item拖动
    private void cofigtouch() {
        touchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
            @Override
            public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                int dragFlags;
                int swipeFlags;
                if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
                    dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
                    swipeFlags = 0;
                } else {
                    dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
                    swipeFlags = 0;
                }
                return makeMovementFlags(dragFlags, swipeFlags);
            }
            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                int fromPosition = viewHolder.getAdapterPosition();//得到拖动ViewHolder的position
                int toPosition = target.getAdapterPosition();//得到目标ViewHolder的position
                Log.e("起始位置",fromPosition+"");
                Log.e("目标位置",toPosition+"");
                //size-1对应正常的最后一位
                if (fromPosition < toPosition)
                {
                    for (int i = fromPosition; i < toPosition; i++) {
                        Collections.swap(workItems, i, i + 1);
                        //记录变更后的位置
                        CacheUtils.getInstance().setWorkItemPosition(workItems);

                    }

                }
                else
                {
                    for (int i = fromPosition; i > toPosition; i--)
                    {
                        Collections.swap(workItems, i, i - 1);
                        //记录变更后的位置
                        CacheUtils.getInstance().setWorkItemPosition(workItems);

                    }
                }
                //设置下交换了位置的集合
                workAdpter.setDatas(workItems);
                workAdpter.notifyItemMoved(fromPosition, toPosition);
                //更新数据位置,不然点击错位
                workAdpter.notifyItemChanged(toPosition,workItems.get(fromPosition));
                return true;
            }
            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

            }

            @Override
            public boolean isLongPressDragEnabled() {
                return false;
            }

            /**
             * 选中时
             * @param viewHolder
             * @param actionState
             */
            @Override
            public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
                if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
                    viewHolder.itemView.setAlpha(0.5f);
                }
                super.onSelectedChanged(viewHolder, actionState);
            }
            /**
             *
             * 未选中
             * @param recyclerView
             * @param viewHolder
             */
            @Override
            public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                super.clearView(recyclerView, viewHolder);
                viewHolder.itemView.setAlpha(1f);
            }


        });
        touchHelper.attachToRecyclerView(recyclerWork);
    }

    @Override
    protected int setlayout() {
        return R.layout.fragment_work;
    }

}
